package jnpf.base.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public interface DynamicMapper {
    @SelectProvider(type = SqlProvider.class, method = "dynamicSql")
    IPage<Map<String, Object>> selectPageByCondition(Page<?> page, @Param("tableName") String tableName, @Param("condition") Map<String, Object> condition);

    class SqlProvider {
        public String dynamicSql( Page<?> page,@Param("tableName") String tableName, @Param("condition") Map<String, Object> condition) {
            SQL sql = new SQL();

            // 构建 SELECT 语句
            sql.SELECT("*").FROM(tableName);

            // 添加 WHERE 条件
            if (condition != null && !condition.isEmpty()) {
                condition.forEach((key, value) -> {
                    sql.WHERE(key + " = #{condition." + key + "}");
                });
            }
// 添加分页条件

            return sql.toString();


        }

    }
}
